package com.vf.admin.client.service.impl;

import com.vf.admin.client.dao.AppGuardDao;
import com.vf.admin.client.dao.AppIntimacyGuardDao;
import com.vf.admin.client.dao.AppSetupDao;
import com.vf.admin.client.dao.AppUserDao;
import com.vf.admin.client.dto.AppRankingDto;
import com.vf.admin.client.service.AppIntimacyGuardService;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.client.service.SystemAlertService;
import com.vf.admin.miaodong.entity.AppGuard;
import com.vf.admin.miaodong.entity.AppIntimacyGuard;
import com.vf.admin.miaodong.entity.AppSetup;
import com.vf.admin.miaodong.entity.AppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class AppIntimacyGuardServiceImpl implements AppIntimacyGuardService {

    @Autowired
    private AppIntimacyGuardDao appIntimacyGuardDao;
    @Autowired
    private AppSetupDao appSetupDao;
    @Autowired
    private AppGuardDao appGuardDao;
    @Autowired
    private AppUserDao appUserDao;
    @Autowired
    private SystemAlertService systemAlertService;
    @Autowired
    private AppUserService appUserService;

    /**
     * 插入记录
     *
     * @param appIntimacyGuard
     */
    @Override
    public AppIntimacyGuard addIntimacyGuard(AppIntimacyGuard appIntimacyGuard) {
        return appIntimacyGuardDao.addIntimacyGuard(appIntimacyGuard);
    }

    /**
     * 增加亲密度并判断是否守护
     *
     * @param appIntimacyGuard
     * @return
     */
    @Override
    public AppIntimacyGuard addOrSaveIntimacyGuard(AppIntimacyGuard appIntimacyGuard) {
        boolean guardSuccess = false;
        AppSetup setup = appSetupDao.get();
        AppGuard guaardByUserId = appGuardDao.getGuaardByUserId(appIntimacyGuard.getUserId(), appIntimacyGuard.getBeiUserId());
        if (guaardByUserId == null) {
            AppGuard appGuard = new AppGuard();
            appGuard.setUserId(appIntimacyGuard.getUserId());
            appGuard.setBeiUserId(appIntimacyGuard.getBeiUserId());
            appGuard.setCreatetime(new Date());
            appGuard.setIntimacy(appIntimacyGuard.getIntimacy());
            appGuard.setGiftMoney(appIntimacyGuard.getGiftMoney());
            //亲密度达到守护标准
            if (setup.getIntimacyGuard().compareTo(appIntimacyGuard.getIntimacy()) <= 0) {
                /**
                 * 如果当前用户没有被守护直接守护
                 * 如果已被他人守护判断是否能够顶替守护者成为这个人的守护
                 */
                AppGuard guardInfo = appGuardDao.getGuardInfo(appIntimacyGuard.getBeiUserId());
                if (guardInfo == null) {
                    appGuard.setIsGuard(1);
                    guardSuccess = true;
                } else {
                    BigDecimal chaochu = guardInfo.getIntimacy().add(guardInfo.getIntimacy().multiply(setup.getGuardZeng()));
                    if (chaochu.compareTo(appIntimacyGuard.getIntimacy()) < 0) {
                        appGuard.setIsGuard(1);
                        appGuardDao.updateGuardById(guardInfo.getId(), 2);
                        guardSuccess = true;
                    } else {
                        appGuard.setIsGuard(2);
                    }
                }
            } else {
                appGuard.setIsGuard(2);
            }
            appGuardDao.add(appGuard);
        } else {
            appGuardDao.incIntimacyById(guaardByUserId.getId(), appIntimacyGuard.getIntimacy(), appIntimacyGuard.getGiftMoney());
            AppGuard guardInfo = appGuardDao.getGuardInfo(appIntimacyGuard.getBeiUserId());
            if (guardInfo != null) {
                BigDecimal chaochu = guardInfo.getIntimacy().add(guardInfo.getIntimacy().multiply(setup.getGuardZeng()));
                if (chaochu.compareTo(appIntimacyGuard.getIntimacy().add(guaardByUserId.getIntimacy())) < 0) {
                    appGuardDao.updateGuardById(guardInfo.getId(), 2);
                    appGuardDao.updateGuardById(guaardByUserId.getId(), 1);
                    guardSuccess = true;
                }
            } else {
                if (setup.getIntimacyGuard().compareTo(appIntimacyGuard.getIntimacy().add(guaardByUserId.getIntimacy())) <= 0) {
                    appGuardDao.updateGuardById(guaardByUserId.getId(), 1);
                    guardSuccess = true;
                }
            }
        }

        if(guardSuccess) {
            //发送小秘书消息
            AppUser appUser = appUserService.get(appIntimacyGuard.getUserId());
            AppUser beiAppUser = appUserService.get(appIntimacyGuard.getBeiUserId());
            systemAlertService.guard(appUser.getTengxuncode(), beiAppUser.getNick());
        }
        return appIntimacyGuard;
    }


    /**
     * 获取亲密信息
     *
     * @param userId
     * @param beiUserId
     * @return
     */
    @Override
    public AppIntimacyGuard getIntimacyGuard(Integer userId, Integer beiUserId) {
        return appIntimacyGuardDao.getIntimacyGuard(userId, beiUserId);
    }

    /**
     * 增加亲密度
     *
     * @param userId
     * @param beiUserId
     * @param intimacy
     */
    @Override
    public void incIntimacyGuard(Integer userId, Integer beiUserId, Double intimacy, Double giftMoney) {
        appIntimacyGuardDao.incIntimacyGuard(userId, beiUserId, intimacy, giftMoney);
    }


    /**
     * 更新守护状态
     *
     * @param userId
     * @param beiUserId
     * @param isGuard   是否守护，1是，2否，默认2否
     */
    @Override
    public void updateGuard(Integer userId, Integer beiUserId, Integer isGuard) {
        appIntimacyGuardDao.updateGuard(userId, beiUserId, isGuard);
    }

    /**
     * 根据id更新守护状态
     *
     * @param id
     * @param isGuard
     */
    @Override
    public void updateGuardById(Integer id, Integer isGuard) {
        appIntimacyGuardDao.updateGuardById(id, isGuard);
    }


    @Override
    public List<AppRankingDto> getRanking(Integer secondType, Map<String, Object> params) {
        ArrayList<AppRankingDto> appRankingDtos = new ArrayList<>();
        switch (secondType) {
            case 1:
                List<AppIntimacyGuard> dayRanking = appIntimacyGuardDao.getDayRanking();
                if (dayRanking != null && dayRanking.size() > 0) {
                    dayRanking.forEach(e -> {
                        AppRankingDto appRankingDto = new AppRankingDto();
                        if (!params.containsKey(e.getUserId())) {
                            AppUser appUserA = appUserDao.get(e.getUserId());
                            appRankingDto.setNickA(appUserA.getNick());
                            appRankingDto.setUserIdA(appUserA.getId());
                            appRankingDto.setPicA(appUserA.getPic());
                        }
                        if (!params.containsKey(e.getBeiUserId())) {
                            AppUser appUserB = appUserDao.get(e.getBeiUserId());
                            appRankingDto.setNickB(appUserB.getNick());
                            appRankingDto.setUserIdB(appUserB.getId());
                            appRankingDto.setPicB(appUserB.getPic());
                            appRankingDto.setTotalIntimacy(e.getTotalIntimacy());
                        }
                        appRankingDtos.add(appRankingDto);
                    });
                    return appRankingDtos;
                } else {
                    return null;
                }
            case 2:
                List<AppIntimacyGuard> weekRanking = appIntimacyGuardDao.getWeekRanking();
                if (weekRanking != null && weekRanking.size() > 0) {
                    weekRanking.forEach(e -> {
                        AppRankingDto appRankingDto = new AppRankingDto();
                        AppUser appUserA = appUserDao.get(e.getUserId());
                        AppUser appUserB = appUserDao.get(e.getBeiUserId());
                        appRankingDto.setNickA(appUserA.getNick());
                        appRankingDto.setNickB(appUserB.getNick());
                        appRankingDto.setUserIdA(appUserA.getId());
                        appRankingDto.setUserIdB(appUserB.getId());
                        appRankingDto.setPicA(appUserA.getPic());
                        appRankingDto.setPicB(appUserB.getPic());
                        appRankingDto.setTotalIntimacy(e.getTotalIntimacy());
                        appRankingDtos.add(appRankingDto);
                    });
                    return appRankingDtos;
                } else {
                    return null;
                }
            case 3:
                List<AppIntimacyGuard> monthRanking = appIntimacyGuardDao.getMonthRanking();
                if (monthRanking != null && monthRanking.size() > 0) {
                    monthRanking.forEach(e -> {
                        AppRankingDto appRankingDto = new AppRankingDto();
                        AppUser appUserA = appUserDao.get(e.getUserId());
                        AppUser appUserB = appUserDao.get(e.getBeiUserId());
                        if (appUserA != null && appUserB != null) {
                            appRankingDto.setNickA(appUserA.getNick());
                            appRankingDto.setNickB(appUserB.getNick());
                            appRankingDto.setUserIdA(appUserA.getId());
                            appRankingDto.setUserIdB(appUserB.getId());
                            appRankingDto.setPicA(appUserA.getPic());
                            appRankingDto.setPicB(appUserB.getPic());
                            appRankingDto.setTotalIntimacy(e.getTotalIntimacy());
                            appRankingDtos.add(appRankingDto);
                        }

                    });
                    return appRankingDtos;
                } else {
                    return null;
                }
            default:
                return null;
        }
//        return appRankingDtos;
    }

}
